<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.autocomplete.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.autocomplete.js"></script>
<script type="text/javascript">
Refresh = function(){
	//jq的自动完成
	$(".ajax_product").autocomplete("<?=url('Product::Bind/ProductInfoAjax')?>").result(function(event, data, formatted){
		if (data[0])
		{
			var container = $(this).parent();
			container.find("input.product_number").val(data['number']);
			container.find("input[name^='product_id']").val(data['id']);
		}
	});
};
$(function(){
	$(".datepicker").datepicker({dateFormat:'yy-mm-dd',changeMonth:true});
	var i = <?=isset($product_bind['item']) ? count($product_bind['item']) : 1?>;
	$("#add_product").live("click",function(){
		var _html = "<tr>";
		_html += "<td><input type='text' size='70' name='product_number_new" + i + "' class='ajax_product' /><input type='hidden' name='product_id_new" + i + "' /></td>";
		_html += "<td width='80'><input type='text' size='7' name='product_quantity_new" + i + "' /></td>";
		_html += "<td width='80'><a href='javascript:void(0);' class='del_product'>删除产品</a></td>";
		_html += "</tr>";
		$(".list_table").append(_html);
		Refresh();
		i++;
	});
	$(".del_product").live("click", function(){
		if ( ! confirm("确定删除该产品？"))
		{
			return false;
		}
		else
		{
			$(this).parent().parent().remove();
		}
	});
	//保存捆绑产品
	$("#add_save").click(function(){
		var can_save = 1;
		if (! $("#type_select").val())
		{
			can_save = 0;
			alert("请选择创建部门");
		}
		$("#list").find("input[name^='product_id_new']").each(function(){
			if (! $(this).val())
			{
				can_save = 0;
				alert("请检查每个产品编号");
			}
		});
		$("#list").find("input[name^='product_quantity_new']").each(function(){
			var value = $(this).val();
			if (! ((/^(\+)?\d+$/).test(value) && value > 0))
			{
				can_save = 0;
				alert("请检查每个产品数量");
			}
		});
		if (can_save)
		{
			$("form").submit();
		}
	});
	Refresh();
});
</script>
<form method="post" action="<?=url('Product::Bind/EditSave')?>">
<h3>捆绑号：<?=$product_bind['number']?></h3>
<p>捆绑名称：<input type="text" name="bind_name" value="<?=$product_bind['name']?>"/></p>
<p>创建部门：
	<select name="type" id="type_select">
		<option value="">请选择</option>
		<?foreach (Q::ini('custom_flag/product_bind_type') as $pdt):?>
		<option value="<?=$pdt['value']?>" <?if ($pdt['value'] == $product_bind['type']):?>selected="selected"<?endif;?>><?=$pdt['name']?></option>
		<?endforeach;?>
	</select>
</p>
<p>创建时间：<?=$product_bind['create_time']?></p>
<input type="hidden" name="product_bind_id" value="<?=$product_bind['id']?>" />
<table class="list_table"  width="60%" id="list">
<tr>
	<th>产品编号</th>
	<th>捆绑数量</th>
	<th>操作</th>
</tr>
<?foreach ($product_bind['item'] as $key => $pbi):?>
<tr>
	<td>
		<input type="text" size="70" name="product_number_new<?=$key?>" value="<?=$pbi['number']?> <?=$pbi['name']?>" class="ajax_product"/>
		<input type="hidden" name="product_id_new<?=$key?>" value="<?=$pbi['product_basic_id']?>" />
	</td>
	<td width="80"><input type="text" size="7" name="product_quantity_new<?=$key?>" value="<?=$pbi['quantity']?>" /></td>
	<td width="80"><a href="javascript:void(0);" class="del_product">删除产品</a></td>
</tr>
<?endforeach;?>
</table>
<a href="javascript:void(0);" id="add_product">添加捆绑产品</a>
<div style="margin-left: 250px;"><input type="button" class="button" value="保存" id="add_save"/></div>
</form>
<?php $this->_endblock();?>